package com.dibang.app.view;

import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dibang.app.R;
import com.dibang.app.mvp.util.DPUtil;
import com.dibang.app.mvp.util.WindowUtil;
import com.dx.dxloadingbutton.lib.LoadingButton;

import am.widget.stateframelayout.StateFrameLayout;

/**
 * Created by wxl on 2019/7/19.
 */

public class ViewHelper {

    /**
     * 设置LoadingButtonBackgroundShader
     * @param button
     */
    public static void setLoadingButtonBackgroundShader(LoadingButton button){
        LinearGradient gradient = new LinearGradient(0f, 0f, 1000f, 100f, getResColor(button.getContext(),R.color.blue_gradient_start_color), getResColor(button.getContext(),R.color.blue_gradient_end_color), Shader.TileMode.CLAMP);
        button.setBackgroundShader(gradient);
    }


    /**
     * 设置LoadingButtonBackgroundShader
     * @param button
     */
    public static void setLoadingButtonBackgroundShader(LoadingButton button,Context context){
        LinearGradient gradient = new LinearGradient(0f, 0f, 1000f, 100f, getResColor(context,R.color.blue_gradient_start_color), getResColor(context,R.color.blue_gradient_end_color), Shader.TileMode.CLAMP);
        button.setBackgroundShader(gradient);
    }


    /**
     * 获得res颜色
     * @param context
     * @param id
     * @return
     */
    @ColorInt
    public static final int getResColor(Context context, @ColorRes int id) {
        return ContextCompat.getColor(context,id);
    }


    /**
     * 布局内容为空时
     * @param stateFrameLayout
     * @param drawableId
     * @param emptyText
     */
    public static final void setStateSimpleEmptyView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId,@NonNull String emptyText){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        ImageView imageView = view.findViewById(R.id.state_simple_empty_image_view);
        TextView textView = view.findViewById(R.id.state_simple_empty_text_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        textView.setText(emptyText);
        stateFrameLayout.setEmptyView(view);
    }


    /**
     * 布局内容为空
     * @param stateFrameLayout
     * @param drawableId
     */
    public static final void setStateSimpleEmptyView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        ImageView imageView = view.findViewById(R.id.state_simple_empty_image_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        stateFrameLayout.setEmptyView(view);
    }

    /**
     * 布局内容为空
     */
    public static final void setStateSimpleEmptyView(@NonNull StateFrameLayout stateFrameLayout, @NonNull String emptyText){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_empty_text_view);
        textView.setText(emptyText);
        stateFrameLayout.setEmptyView(view);
    }

    /**
     * 布局内容为错误
     */
    public static final void setStateSimpleErrorView(@NonNull StateFrameLayout stateFrameLayout, @NonNull String errorText){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_empty_text_view);
        textView.setText(errorText);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 布局内容为错误
     */
    public static final void setStateSimpleErrorView(@NonNull StateFrameLayout stateFrameLayout, @NonNull String errorText,@NonNull View.OnClickListener onClickListener){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_empty_text_view);
        textView.setText(errorText);
        view.setOnClickListener(onClickListener);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 布局内容为错误时
     * @param stateFrameLayout
     * @param drawableId
     * @param
     */
    public static final void setStateSimpleErrorView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId,@NonNull String errorText,@NonNull View.OnClickListener onClickListener){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_empty_text_view);
        textView.setText(errorText);
        ImageView imageView = view.findViewById(R.id.state_simple_empty_image_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        view.setOnClickListener(onClickListener);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 布局内容为错误
     * @param stateFrameLayout
     * @param drawableId
     */
    public static final void setStateSimpleErrorView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        ImageView imageView = view.findViewById(R.id.state_simple_empty_image_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        stateFrameLayout.setErrorView(view);
    }


    /**
     * 布局内容为加载中时
     * @param stateFrameLayout
     * @param drawableId
     * @param
     */
    public static final void setStateSimpleLoadingView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId,@NonNull String errorText){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_loading_text_view);
        textView.setText(errorText);
        ImageView imageView = view.findViewById(R.id.state_simple_loading_image_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        ProgressBar bar = view.findViewById(R.id.state_simple_loading_bar_view);
        bar.setVisibility(View.GONE);
        stateFrameLayout.setErrorView(view);
    }



    /**
     * 布局内容为加载中时
     * @param stateFrameLayout
     * @param drawableId
     * @param
     */
    public static final void setStateSimpleLoadingView(@NonNull StateFrameLayout stateFrameLayout, @DrawableRes int drawableId){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        ImageView imageView = view.findViewById(R.id.state_simple_loading_image_view);
        imageView.setBackgroundResource(drawableId);
        imageView.setVisibility(View.VISIBLE);
        ProgressBar bar = view.findViewById(R.id.state_simple_loading_bar_view);
        bar.setVisibility(View.GONE);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 布局内容为加载中时
     * @param stateFrameLayout
     */
    public static final void setStateSimpleLoadingView(@NonNull StateFrameLayout stateFrameLayout,@NonNull String errorText){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        TextView textView = view.findViewById(R.id.state_simple_loading_text_view);
        textView.setText(errorText);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 布局内容为加载中时
     * @param stateFrameLayout
     */
    public static final void setStateSimpleLoadingView(@NonNull StateFrameLayout stateFrameLayout){
        View view = LayoutInflater.from(stateFrameLayout.getContext()).inflate(R.layout.state_simple_empty_layout, null, false);
        stateFrameLayout.setErrorView(view);
    }

    /**
     * 设置RecyclerView 单行显示
     * @param view
     */
    public static final void setRecyclerViewSingleLineManager(RecyclerView view){
        GridLayoutManager manager = new GridLayoutManager(view.getContext(),1, GridLayoutManager.HORIZONTAL,false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        view.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 10);
        view.setLayoutManager(manager);
        view.setNestedScrollingEnabled(false);
        view.setHasFixedSize(true);
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);
    }


    /**
     * 设置RecyclerView
     * @param view
     */
    public static final void setRecyclerViewGridLayoutManager(RecyclerView view,int spanCount){
        GridLayoutManager manager = new GridLayoutManager(view.getContext(),spanCount, GridLayoutManager.VERTICAL,false);
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        view.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 10);
        view.setLayoutManager(manager);
        view.setNestedScrollingEnabled(false);
        view.setHasFixedSize(true);
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);
    }


    /**
     * 设置RecyclerView
     * @param view
     */
    public static final void setRecyclerViewLinearLayoutManager(RecyclerView view){
        LinearLayoutManager manager = new LinearLayoutManager(view.getContext());
        manager.setSmoothScrollbarEnabled(true);
        manager.setAutoMeasureEnabled(true);
        RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
        view.setRecycledViewPool(viewPool);
        viewPool.setMaxRecycledViews(0, 10);
        view.setLayoutManager(manager);
        view.setNestedScrollingEnabled(false);
        view.setHasFixedSize(true);
        view.setFocusable(false);
        view.setFocusableInTouchMode(false);
    }


    /**
     * 生成一个宽度铺满屏幕宽度的PopupWindowBuilder
     * @param height
     * @return
     */
    public static final RxPopWindow.PopupWindowBuilder generatePopupWindowBuilder(int height,Context context){
        return new RxPopWindow.PopupWindowBuilder(context)
                .setFocusable(true)
                .setOutsideTouchable(true)
                .enableBackgroundDark(true)
                .size(WindowUtil.getScreenWidth(context), DPUtil.dip2px(context, height))
                //.setAnimationStyle(R.style.CustomPopWindowStyle) // 添加自定义显示和消失动画
                ;
    }

}
